package tech.aistar.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import tech.aistar.mapper.ProductMapper;
import tech.aistar.model.entity.Product;
import tech.aistar.model.vo.ProductQueryVo;
import tech.aistar.service.IProductService;
import tech.aistar.util.MyBatisUtil;

import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/4/21 1:59 下午
 */
@SuppressWarnings("all")
public class ProductServiceImpl implements IProductService {
    @Override
    public PageInfo<Product> findAll(Integer pageNow, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);

        //分页插件底层使用到了拦截器
        //在真实开发中,如果数据量比较大,我们是需要重写这个插件的,数据量比较大的时候
        //这个分页插件的查询性能极其性能.

        //手写分页公式 limit (pageNow-1)*pageSize,pageSize;
        PageHelper.startPage(pageNow,pageSize);
        List<Product> productList = productMapper.findAll();
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        MyBatisUtil.closeSqlSession(sqlSession);
        return pageInfo;
    }

    @Override
    public PageInfo<Product> findAll(ProductQueryVo vo, Integer pageNow, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);

        //分页插件底层使用到了拦截器
        //在真实开发中,如果数据量比较大,我们是需要重写这个插件的,数据量比较大的时候
        //这个分页插件的查询性能极其性能.

        //手写分页公式 limit (pageNow-1)*pageSize,pageSize;
        PageHelper.startPage(pageNow,pageSize);
        List<Product> productList = productMapper.findByConditional(vo);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        MyBatisUtil.closeSqlSession(sqlSession);
        return pageInfo;
    }

    @Override
    public Product getById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
        Product product = productMapper.getById(id);
        MyBatisUtil.closeSqlSession(sqlSession);
        return product;
    }
}
